/*=========================================================================================================================================

								  DOCUMENTATION
							
					
					USAGE IN			:		VFS PROJECT, GROUP 31
					
					OPERATION TYPE		:		FILE OPERATION
					
					NAME OF FILE		:		export_file.c
					
					NAME OF OPERATION	:		exportfile(P1, P2)
					
												- P1: source file path (in VFS)
												- P2: output file path excluding filename(in Hard Disk)
					
					FORMALY CALLED AS	:		exportfile_31(source_file_path, output_file_path)
					
					VERSION				:		1.0
					
					CREATED BY			:		MADHULIKA 
					
					TESTED BY			:		ANUJ DUGGAL, MADHULIKA, SURYA
					
					
					TODO				:		Define a function that accepts two arguments as mentioned above, and does the following:
												
												VALIDATIONS (APPLYING ERROR CODES):
													- mounted, 
													- source file existance (in VFS),
													- output disk path existance (in Hard Disk), 
													- output file is a text file
													
												TASK:
													- Search the source file in N-ARY
													- Check if we are exporting directory or file
													- Read the contents of the file (File which is in VFS)
													- Export the file read above into an output disk path (on Hard disk)
													
					OTHER FUNCTIONS		:		- validateMountState()
					CALLED						- search_nary(file_path)
												- validateFileExistance(out_path) //actually validating the disk path existance
												


==========================================================================================================================================*/
#include"common_headers.h"
int exportfile31(char *vfs_file_path, char *hard_disk_path)
{

	/* 
	
	REFERENCES AND ASSUMPTIONS:
	
		vfs_file_path = source file path
		hard_disk_path = output file path

	*/
	
	//DECLARATIONS
    int collect=0, is_mounted, is_source_file_exist, is_output_file_exist, is_exporting_file, m, found, pos;
    FILE *fptr, *fpdisk;
    char source_file_path[100], disk_file_path[100];
    char filename[100];
    char data[1024];
    struct BST_Node *node;
    struct Block *block;
    struct FileDescriptor *fd_desc1=NULL;
   
    //INITIALIZATIONS
    collect = 0;
    is_mounted = 0;
    is_source_file_exist = 0;
    is_output_file_exist = 0;
    is_exporting_file = 0;
    found = 0;
    
    node = search(bst_root,vfs_file_path);
    strcpy(source_file_path, vfs_file_path);
    strcpy(disk_file_path, hard_disk_path);
  
  	//CHECK IF VFS MOUNTED
    is_mounted = validateMountState();
    if(is_mounted)
    {
    	//CHECK SOURCE FILE EXISTANCE (DONE IN VFS)
		is_source_file_exist = search_nary(source_file_path);
		if(is_source_file_exist)		
		{
			//CHECK IF DISK PATH EXISTS
			is_output_file_exist = validateFileExistance(disk_file_path);	
			if(is_output_file_exist)
			{
				//CHECK IF WE ARE EXPORTING DIRECTORY OR A FILE (EXPORTING FILE ONLY IS ALLOWED)
				is_exporting_file = strcmp(node->data->filetype,"");
				if( is_exporting_file != 0 )
				{
					fptr=fopen(mainHeader.filelabel,"rb+");
					if(fptr!=NULL)
					{
						for(m=0; m < mainHeader.maximumFileDescriptor; m++)
						{
							fd_desc1=&fileDescriptors[m];
				
							//CHECK IF FOUND THE FILE
							if(freelist[m]==1 && strcmp(fd_desc1->locationpath,vfs_file_path)==0 )
							{
								found=1;
								pos=m;
								break;
							}
						}				
						
						//FILE SUCCESSFULLY FOUND		
						if(found==1)	
						{
							//SEEKING THE VFS TILL THE END OF THE OCCUPIED SPACE
							fseek(fptr,(sizeof(struct MainHeader)+(mainHeader.maximumFileDescriptor*sizeof(char))+(mainHeader.maximumFileDescriptor*sizeof(struct FileDescriptor))+(pos*sizeof(struct Block))),SEEK_SET);
							block=(struct Block*) malloc (sizeof(struct Block));
							fread(block->block,1024,1,fptr);
							fclose(fptr);
							
							//READING THE DATA FROM THE FILE (FILENAME MENTIONED IN THE FORMAL ARGUMENTS ALONG WITH VFS_PATH [SOURCE FILE PATH] )
							strcpy(filename,node->data->filename);
							
							//CONCATENATING '/' AND HENCE THE FILENAME AT THE END OF THE DIRECTORY PATH
							strcat(hard_disk_path,"/");
							strcat(hard_disk_path,filename);
							
							//WRITING TO THE FILE, THE DETAILS READ FROM THE BLOCK 
							if ((fpdisk = fopen(hard_disk_path,"wb"))!=NULL)
							{
							    strcpy(data,block->block);
							    fwrite(data,strlen(data),1,fpdisk);
								fclose(fpdisk);
							    collect = 1;
							    printf("exportfile_SUCCESS\n");
							}
							else
							{
							    //CANNOT_CREATE_SPECIFIED_OUTPUTFILE
							    printf("exportfile_FAILURE %s\n", ERR_VFS_EXPORTFILE_02);							    
							}					
						}
						else
						{
							//MAY BE NOT NEEDED (COZ V R ALREADY KEEPING A CHECK TO SEE IF SOURCE FILE EXISTS IN THE VFS)
							printf("NO ENTRY IN FREE LIST , FILE NOT FOUND");
						}						
					}
					else
					{
						//FILE COULD NOT BE READ
						printf("exportfile_FAILURE %s\n", ERR_VFS_EXPORTFILE_09);
					}
				}
				else
				{
					//CANNOT_EXPORT_DIR
					printf("exportfile_FAILURE %s\n", ERR_VFS_EXPORTFILE_06);
				}
			}
			else
			{
				//CANNOT_FIND_SPECIFIED_DESTINATION_PATH (or) INVALID SPECIFIED DISK PATH
				printf("exportfile_FAILURE %s\n", ERR_VFS_EXPORTFILE_08);
			}
		}
		else
		{
			//CANNOT_FIND_SPECIFIED_SOURCEFILE
			printf("exportfile_FAILURE %s\n", ERR_VFS_EXPORTFILE_01);
		}
    }
    else
    {
    	//VFS_NOT_MOUNTED
    	printf("exportfile_FAILURE %s\n", ERR_VFS_EXPORTFILE_07);	
    }
    
    return collect;
    
}

